
package com.ifcar.app.util;

import java.util.List;

import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;

import com.netease.util.Pair;

/**
 * 分组Adapter基类<br/>
 * 
 * @author wjying
 * @param <T>
 * @param <S>
 */
public abstract class GroupAdapter<T, S> extends BaseAdapter {
    private List<Pair<T, List<S>>> mList;

    public GroupAdapter(List<Pair<T, List<S>>> list) {
        mList = list;
    }

    @Override
    public final int getCount() {
        int count = mList.size();
        for (Pair<T, List<S>> pair : mList) {
            count += (null==pair.second?0:pair.second.size());
        }
        return count;
    }

    @Override
    public final S getItem(int position) {
        ItemPosition itemPosition = getRealPosition(position);
        if (itemPosition.pos == -1) {
            return null;
        }
        return mList.get(itemPosition.group).second.get(itemPosition.pos);
    }
    
    @Override
    public final long getItemId(int position) {
        return 0;
    }

    @Override
    public final View getView(int position, View convertView, ViewGroup parent) {
        ItemPosition itemPosition = getRealPosition(position);
        if (itemPosition.pos == -1) {
            return getGroupView(itemPosition.group, convertView, parent);
        } else {
            return getChildView(itemPosition.group, itemPosition.pos, convertView, parent);
        }
    }

    @Override
    public int getItemViewType(int position) {
        return isGroup(position) ? 1 : 0;
    }

    @Override
    public int getViewTypeCount() {
        return 2;
    }

    @Override
    public boolean isEnabled(int position) {
        return !isGroup(position);
    }

    @Override
    public boolean areAllItemsEnabled() {
        return false;
    }

    /**
     * 判断这个位置是否是group
     * 
     * @param position
     * @return
     */
    private boolean isGroup(int position) {
        ItemPosition itemPosition = getRealPosition(position);
        return itemPosition.pos == -1;
    }

    /**
     * 返回item真实位置
     * 
     * @param position
     * @return
     */
    public final ItemPosition getRealPosition(int position) {
        ItemPosition itemPosition = new ItemPosition();
        int groupPosition = 0;
        final int N = mList.size();
        for (int i = 0; i < N; i++) {
            itemPosition.group = i;
            if(null!=mList.get(i).second){
                final int size = mList.get(i).second.size();
                if ((groupPosition + size) >= position) {
                    itemPosition.pos = position - groupPosition - 1;
                    break;
                } else {
                    groupPosition += (size + 1);
                }
            }
            
        }
        return itemPosition;

    }

    /**
     * 返回在apater中一组group的起始位置
     * 
     * @param group
     * @return
     */
    public final int getGroupViewPosition(int group) {
        if (group < 0 || group >= mList.size()) {
            return -1;
        }
        int position = 0;
        for (int i = 0; i < group; i++) {
            position += mList.get(i).second.size();
            position++;
        }
        return position;
    }
    
    /**
     * 根据T返回组的位置
     * @param t
     * @return
     */
    public final int getGroupPosition(T t) {
        final int N = mList.size();
        for (int i = 0; i < N; i++) {
            if (mList.get(i).first.equals(t)) {
                return i;
            }
        }
        return -1;
    }

    public final S getChild(int groupPosition, int childPosition) {
        if(groupPosition<mList.size() && childPosition<mList.get(groupPosition).second.size()){
            try{
                return mList.get(groupPosition).second.get(childPosition);
            }catch(Exception ex){
                return null;
            }
            
        }else{
            return null;
        }
        
    }

    public long getChildId(int groupPosition, int childPosition) {
        return 0;
    }

    public abstract View getChildView(int groupPosition, int childPosition, View convertView,
            ViewGroup parent);

    public final int getChildrenCount(int groupPosition) {
        return mList.get(groupPosition).second.size();
    }

    public final T getGroup(int groupPosition) {
        return mList.get(groupPosition).first;
    }

    public final int getGroupCount() {
        return mList.size();
    }

    public long getGroupId(int groupPosition) {
        return 0;
    }

    public abstract View getGroupView(int groupPosition, View convertView, ViewGroup parent);

    @Override
    public final boolean hasStableIds() {
        return false;
    }

    public static class ItemPosition {
        public int group;

        // 如果pos<0，说明是group
        public int pos = -1;
    }
}
